Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Proxmox/LXC memory conversion Lxc.pm #869

Merged
merged 3 commits into from
Mar 12, 2025

Conversation

Jo-NMS
Copy link
Contributor

@Jo-NMS Jo-NMS commented Mar 11, 2025

Issue:
The glpi-agent was incorrectly handling memory values for LXC containers on hosts using Proxmox.
Proxmox returns a raw byte value (e.g., "10737418240") without a unit suffix, and the current logic
in getCanonicalSize() immediately returns such plain numeric values. This leads to storing the
value in bytes instead of the expected megabytes (MB), potentially causing an overflow in an
unsigned int database column.

Solution:

  • For values ending with a unit (K, M, G, T, or P), the code appends "b" (e.g., "16384M" becomes "16384Mb")
    so that getCanonicalSize() processes them correctly.
  • For plain numeric values (e.g., "10737418240"), the code now detects them using a regex and converts
    the value from bytes to MB by dividing by (1024 * 1024).

Changes:

  • Added an elsif branch to check if the memory value is a plain number using /^\d+$/.
  • For these numeric values, the code converts them to MB via:
    int($val / (1024 * 1024))
  • Retained the existing logic for values with a unit suffix to preserve backward compatibility.

Fix memory conversion for LXC containers on Proxmox by converting plain byte values to MB
@g-bougard
Copy link
Member

Hi @Jo-NMS

can you also fix tests ?

You should just have to fix 2 values in t/tasks/inventory/virtualization/lxc.t, on lines 26 & 58. On l.26, you have to replace '2048000' by 1... yes, 2048000 bytes are rounded to 1Mb. And l.58, you can replace 2147483648 by 2048 which seems more coherent than the previous value.

Update expected memory values in t/tasks/inventory/virtualization/lxc.t to match
the new conversion logic:
- On line 26, change '2048000' to '1' (2048000 bytes round to 1 MB).
- On line 58, change '2147483648' to '2048' for better consistency.
@g-bougard g-bougard merged commit 7430218 into glpi-project:develop Mar 12, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants